﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
/// <summary>
/// SQLHelper 的摘要说明
/// </summary>
public class SQLHelper
{
    private static string sqlconn = System.Configuration.ConfigurationManager.AppSettings["strConn"].ToString().Trim();
	public SQLHelper()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}
    protected SqlConnection GetConn()
    {
        SqlConnection conn = new SqlConnection(sqlconn);
        return conn;
    }
    ///command对象的executenonquery方法
    protected int ExeNonQuery(string sql)
    {
        SqlConnection conn = GetConn();
        try
        {

            conn.Open();
            SqlCommand comm = new SqlCommand(sql, conn);
            return comm.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }
    ///command对象的ExecuteScalar方法
    protected int ExeScalar(string sql)
    {
        SqlConnection conn = GetConn();
        try
        {

            conn.Open();
            SqlCommand comm = new SqlCommand(sql, conn);
            return (int)comm.ExecuteScalar();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }
    ///command对象的ExecuteReader方法
    protected SqlDataReader ExeReader(string sql)
    {
        SqlConnection conn = GetConn();
        try
        {

            conn.Open();
            SqlCommand comm = new SqlCommand(sql, conn);
            return comm.ExecuteReader();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }

    }

    protected DataSet GetDs(string sql)
    {
        SqlConnection conn = GetConn();
        try
        {
            SqlDataAdapter adp = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            return ds;
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }
}